package com.jokins.admin.vue.gitlog.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jokins.admin.vue.gitlog.param.GitPushLogsParam;
import com.jokins.admin.vue.gitlog.param.SaveGitPushParam;
import com.jokins.admin.vue.gitlog.service.GitWebhookService;
import com.jokins.admin.vue.gitlog.vo.GitPushLogsVo;
import com.jokins.common.util.ResultData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/git/webhook")
public class GitWebhookController {

    @Autowired
    private GitWebhookService gitWebhookService;

    /**
     * 接收 gitee java webhook
     * @param payload
     * @return
     */
    @PostMapping("/gitee/likeJavaProject")
    public ResponseEntity<?> SaveHandleGithubWebhook(@RequestBody Map<String, Object> payload) {
        log.info("Received Gitee Webhook Request[Gitee-Java-Project入参]: " + payload);
        SaveGitPushParam param = new SaveGitPushParam();
        param.setPayload(payload);
        return gitWebhookService.SaveHandleGithubWebhook(param);
    }


    /**
     * 获取 gitee java webhook
     * @param applyName
     * @return
     */
    @GetMapping("/logsList")
    public ResultData getGiteeLogsList(@RequestParam String applyName) {
        GitPushLogsParam param = new GitPushLogsParam();
        param.setApplyName(applyName);
        final Page<Object> page = PageHelper.startPage(param.getCurrentPage(), param.getPageSize(), true);
        List<GitPushLogsVo> userList= gitWebhookService.getGiteeLogsList(param);
        return ResultData.pageResponse(userList, page.getTotal(), page.getPages(), page.getPageNum());
    }
} 